<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
const tsKeyId = user.primaryKey.tsSampleValues[0];
_%>
<%_ if (clientTestFrameworkVitest) { _%>
import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
<%_ } _%>
import { ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
<%_ if (enableTranslation) { _%>
import { TranslateModule } from '@ngx-translate/core';
<%_ } _%>
import { of } from 'rxjs';

import { Authority } from 'app/config/authority.constants';
import { UserManagementService } from '../service/user-management.service';
import { User } from '../user-management.model';

import UserManagementUpdate from './user-management-update';

describe('User Management Update Component', () => {
  let comp: UserManagementUpdate;
  let fixture: ComponentFixture<UserManagementUpdate>;
  let service: UserManagementService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      <%_ if (enableTranslation) { _%>imports: [TranslateModule.forRoot()],<%_ } _%>
      providers: [
        provideHttpClient(),
        {
          provide: ActivatedRoute,
          useValue: {
            data: of({ user: new User(<%- tsKeyId %>, 'user', 'first', 'last', 'first@last.com', true, 'en', [Authority.USER], 'admin') }),
          },
        },
      ],
    });
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(UserManagementUpdate);
    comp = fixture.componentInstance;
    service = TestBed.inject(UserManagementService);
  });

  describe('OnInit', () => {
    it('should load authorities and language on init', inject([], () => {
      // GIVEN
      <%= clientTestFramework %>.spyOn(service, 'authorities').mockReturnValue(of(['USER']));

      // WHEN
      comp.ngOnInit();

      // THEN
      expect(service.authorities).toHaveBeenCalled();
      expect(comp.authorities()).toEqual(['USER']);
    }));
  });

  describe('save', () => {
    it('should call update service on save for existing user', inject([], () => {
      // GIVEN
      const entity = { id: <%- tsKeyId %> };
      <%= clientTestFramework %>.spyOn(service, 'update').mockReturnValue(of(entity));
      comp.editForm.patchValue(entity);
      // WHEN
      comp.save();

      // THEN
      expect(service.update).toHaveBeenCalledWith(expect.objectContaining(entity));
      expect(comp.isSaving()).toEqual(false);
    }));

    it('should call create service on save for new user', inject([], () => {
      // GIVEN
      const entity = { login: 'foo' } as User;
      <%= clientTestFramework %>.spyOn(service, 'create').mockReturnValue(of(entity));
      comp.editForm.patchValue(entity);
      // WHEN
      comp.save();

      // THEN
      expect(comp.editForm.getRawValue().id).toBeNull();
      expect(service.create).toHaveBeenCalledWith(expect.objectContaining(entity));
      expect(comp.isSaving()).toEqual(false);
    }));
  });
});
